package com.sumer.psychology.blog.infrastructure.messaging.rabbitmq;

import com.sumer.psychology.blog.infrastructure.persistence.mongo.MongoCategoryListRepository;
import com.sumer.psychology.blog.infrastructure.persistence.mongo.MongoCategoryRepository;
import com.sumer.psychology.common.port.adapter.messaging.Exchanges;
import com.sumer.psychology.common.port.adapter.messaging.rabbitmq.ExchangeListener;

/**
 * 监听目录创建
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/10/15
 * Time: 9:56
 */
public class CategoryCreatedListener extends ExchangeListener {
    private MongoCategoryRepository mongoCategoryRepository;
    private MongoCategoryListRepository mongoCategoryListRepository;

    public CategoryCreatedListener(MongoCategoryRepository mongoCategoryRepository,
                                   MongoCategoryListRepository mongoCategoryListRepository) {
        this.mongoCategoryRepository = mongoCategoryRepository;
        this.mongoCategoryListRepository = mongoCategoryListRepository;
    }

    @Override
    protected String exchangeName() {
        return Exchanges.DEFAULT_EXCHANGE_NAME;
    }

    @Override
    protected void filteredDispatch(String aType, String aTextMessage) {
        mongoCategoryRepository.add(aTextMessage);
        mongoCategoryListRepository.add(aTextMessage);
    }

    @Override
    protected String[] listensTo() {
        return new String[]{"com.sumer.psychology.blog.domain.model.CategoryCreatedEvent"};
    }
}
